
import os
import json
# 12.21 遍历字典
 
def print_tofile(*args):   # 打印日志
    msg = ''
    for x in args:
        msg += str(x) + ' '
    logfilename = 'log_.log'
    with open(logfilename, 'a') as f:
        print(msg, file=f)
    print(msg)  

result_json = []
def get_jsonkey(jsondata):
    if isinstance(jsondata, dict):
        for k ,v in jsondata.items():
            if len(str(v))>30:
                result_json.append('  {} -> {}'.format(k,(str(v)[:30])))
            else:
                result_json.append('  {} -> {}'.format(k,v))
            get_jsonkey(v)
    elif isinstance(jsondata, list):
        for j in jsondata:
            get_jsonkey(j)
    return
    
if __name__ == "__main__":   
    filepath = input("目录:")
    if filepath =="":
        filepath = 'K:\Server\src\界面5.9\client_data' 
    files = []
    filelist = os.listdir(filepath)
    for file in filelist:
        if file.endswith('.json'):
            files.append(file)   
    for x in files:
        file = os.path.join(filepath,x)
        print_tofile(file)
        result_json.clear()
        with open(file, 'r') as f:
            jsondata = json.loads(f.read())               
            get_jsonkey(jsondata)  
            for x in result_json:
                print_tofile(x)